package com.nathan.crm.controller;

import com.nathan.base.BaseController;
import com.nathan.base.ResultInfo;
import com.nathan.crm.exceptions.ParamsException;
import com.nathan.crm.model.UserModel;
import com.nathan.crm.query.UserQuery;
import com.nathan.crm.service.UserService;
import com.nathan.crm.utils.LoginUserUtil;
import com.nathan.crm.vo.SaleChance;
import com.nathan.crm.vo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("user")
public class UserController extends BaseController {
    @Autowired
    private UserService userService;

    /**
     * 登录
     * @param userName
     * @param userPwd
     * @return
     */
    @PostMapping("login")
    @ResponseBody
    public ResultInfo userLogin(String userName,String userPwd){
        ResultInfo resultInfo = new ResultInfo();

        //调用service层的登录方法
        UserModel userModel = userService.userLogin(userName, userPwd);

        resultInfo.setResult(userModel);

        return resultInfo;
    }

    /**
     * 打开修改密码页面
     * @return
     */
    @RequestMapping("toPasswordPage")
    public String toPasswordPage(){
        return "user/password";
    }

    /**
     * 用户密码修改
     * @param request
     * @param oldPassword
     * @param newPassword
     * @param repeatPassword
     * @return
     */
    @PostMapping("updatePwd")
    @ResponseBody
    public ResultInfo updateUserPassword(HttpServletRequest request,
                                String oldPassword,String newPassword,String repeatPassword){

        ResultInfo resultInfo = new ResultInfo();

        //获取Cookie中的userId
        int userId = LoginUserUtil.releaseUserIdFromCookie(request);
        //调用Service层的修改密码方法
        userService.updateUserPassword(userId,oldPassword,newPassword,repeatPassword);

        return resultInfo;
    }

    /**
     * 查询所有角色为销售的用户
     * @return
     */
    @RequestMapping("selectAllSales")
    @ResponseBody
    public List<Map<String,Object>> selectAllSales(){
        return userService.selectAllSales();
    }

    /**
     * 多条件分页查询用户列表
     * @param userQuery
     * @return
     */
    @RequestMapping("list")
    @ResponseBody
    public Map<String,Object> queryByParams(UserQuery userQuery){
        return userService.queryByParamsForTable(userQuery);
    }

    /**
     * 视图跳转到user.ftl（用户管理页面）
     * @return
     */
    @RequestMapping("index")
    public String index(){
        return "user/user";
    }

    /**
     *  添加用户
     * @param user
     * @return
     */
    @RequestMapping("add")
    @ResponseBody
    public ResultInfo addUser(User user){
        userService.addUser(user);

        return success("添加用户成功！");
    }

    /**
     *  更新用户
     * @param user
     * @return
     */
    @RequestMapping("update")
    @ResponseBody
    public ResultInfo updateUser(User user){
        userService.updateUser(user);

        return success("更新用户功！");
    }

    /**
     * 删除一条或多条用户
     * @param ids
     * @return
     */
    @PostMapping("delete")
    @ResponseBody
    public ResultInfo deleteUser(Integer[] ids){
        userService.deleteUser(ids);

        return success("删除成功！");
    }

    /**
     * 打开用户添加或更新页面
     * @param id 用户id
     * @param model
     * @return
     */
    @RequestMapping("addOrUpdateUserPage")
    public String addOrUpdateUserPage(Integer id, Model model){
        // 判断id是否有值
        if (null != id){    // 有值，表示进行更新操作
            // 根据id查询对应的用户数据添加到域对象中
            User user = userService.selectByPrimaryKey(id);
            model.addAttribute("userInfo",user);
        }

        return "user/add_update";
    }

    /**
     * 查询所有角色为客户经理的用户
     * @return
     */
    @RequestMapping("queryAllCustomerManager")
    @ResponseBody
    public List<Map<String,Object>> queryAllCustomerManager(){
        return userService.queryAllCustomerManager();
    }

    /**
     * 跳转到用户信息页面
     * @param request
     * @return
     */
    @RequestMapping("toUserMsgPage")
    public String toUserMsgPage(HttpServletRequest request,Model model){
        // 从cookie中获取登录用户id
        Integer userId = LoginUserUtil.releaseUserIdFromCookie(request);
        User userInfo = userService.selectByPrimaryKey(userId);

        model.addAttribute("userInfo",userInfo);

        return "user/user_msg";
    }

}
